package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.DhClubearningsInfoDao;
import io.renren.modules.demo.dto.DhClubearningsInfoDTO;
import io.renren.modules.demo.entity.DhClubearningsInfoEntity;
import io.renren.modules.demo.service.DhClubearningsInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@Service
public class DhClubearningsInfoServiceImpl extends CrudServiceImpl<DhClubearningsInfoDao, DhClubearningsInfoEntity, DhClubearningsInfoDTO> implements DhClubearningsInfoService {

    @Autowired
    private DhClubearningsInfoDao dhClubearningsInfoDao;

    @Override
    public QueryWrapper<DhClubearningsInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhClubearningsInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public DhClubearningsInfoDTO getDataListByOrderId(Integer orderId) {
        LambdaQueryWrapper<DhClubearningsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhClubearningsInfoEntity::getOrderId, orderId);
        List<DhClubearningsInfoEntity> list = dhClubearningsInfoDao.selectList(queryWrapper);
        DhClubearningsInfoDTO dhClubearningsInfoDTO = new DhClubearningsInfoDTO();
        for (DhClubearningsInfoEntity dhClubearningsInfoEntity : list) {
            dhClubearningsInfoDTO.setOrderId(dhClubearningsInfoEntity.getOrderId());
            dhClubearningsInfoDTO.setEarnings(dhClubearningsInfoEntity.getEarnings());
            dhClubearningsInfoDTO.setCreateTime(dhClubearningsInfoEntity.getCreateTime());
        }
        return dhClubearningsInfoDTO;
    }

    /**
     * 获取俱乐部总收益
     *
     * @return
     */
    @Override
    public Integer getChildFiveData() {
        return dhClubearningsInfoDao.getChildFiveData();
    }

    /**
     * 获取近五年俱乐部收益数据
     *
     * @return
     */
    @Override
    public BigDecimal getChildCenterData(String year) {
        BigDecimal earnings = BigDecimal.valueOf(0);
        String yearStart = year.concat("-01-01 00:00:00");
        String yearEnd = year.concat("-12-31 23:59:59");
        LambdaQueryWrapper<DhClubearningsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().between(DhClubearningsInfoEntity::getCreateTime, yearStart, yearEnd);
        List<DhClubearningsInfoEntity> list = dhClubearningsInfoDao.selectList(queryWrapper);
        for (DhClubearningsInfoEntity dhClubearningsInfoEntity : list) {
            earnings = earnings.add(dhClubearningsInfoEntity.getEarnings());
        }
        return earnings;
    }
}